import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
import datetime
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)


    def test_shop_list(self):
        #商品列表测试
        #点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        #定位商品子框架
        shop_iframe=self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #进入商品子框架
        self.browser.switch_to.frame(shop_iframe)
        
        #定位商品名称并输入数据
        self.browser.find_element(By.ID,'keyword').send_keys('文具')
        #定位查询按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/button[1]').click()
        sleep(1)
        #获取商品列表
        shop_list=self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
    
        if len(shop_list)==0:
            print("没有数据!!")
        else:
            is_not_bug=True
            for item in shop_list:
                shop_name=item.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in shop_name:
                    is_not_bug = False
            if is_not_bug:
                print("没有问题")
            else:
                print("有bug")
        
    def test_1(self):
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')

        self.browser.find_element(By.NAME, 'password').send_keys('123456')
        
        self.browser.find_element(By.NAME, 'code').send_keys('1111')
        
        self.browser.find_element(By.ID, 'login').click()
        
        sleep(2)
        
    def test_shop_add(self):
        #点击商品按钮
        # self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a').click()
        # sleep(2)
       
        #回到主框架
        self.browser.switch_to.default_content()
        #定位商品子框架
        iframe1=self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #进入商品子框架
        self.browser.switch_to.frame(iframe1)
        #点击发布商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        #定义商品发布子框架
        shop_add_iframe = self.browser.find_element(By.XPATH,"/html/body/div[3]/div[2]/iframe")
        self.browser.switch_to.frame(shop_add_iframe)
        #定位商品名称输入框并输入数据
        now = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('测试商品'+now)
        #定位第一个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        #定位第一个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        #定位第二个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        #定位第二个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        #定位第三个大类的下拉框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        #定位第三个大类的下拉数据并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(3)
        #定位商品主图，图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(3)
        #进入图片上传子框架
        upload_img_iframe_1=self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_1)
        self.test_upload_img('E:\\软件测试\\1.jpg')
        
        #回到主框架
        self.browser.switch_to.default_content()
        #再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        #再进入商品发布
        self.browser.switch_to.frame(shop_add_iframe)
        #定位商品轮播图 图片上传按钮
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        #进入图片上传子框架
        upload_img_iframe_2=self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
        self.browser.switch_to.frame(upload_img_iframe_2)
        self.test_upload_img('E:\\软件测试\\1.jpg')
        
        #回到主框架
        self.browser.switch_to.default_content()
        #再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        #再进入商品发布
        self.browser.switch_to.frame(shop_add_iframe)
        
        #点击价格库存
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        #定位价格、成本价、库存并输入数据
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('2')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('2')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        
        #回到商品子框架进行保存
        #回到主框架
        self.browser.switch_to.default_content()
        #再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        #保存
        self.browser.find_element(By.LINK_TEXT,'保存').click()
        sleep(4)
        

        
    
        
    def test_upload_img(self,file_path):
        #上传图片
        sleep(1)
        self.browser.find_element(By.XPATH,"//input[@type='file']").send_keys(file_path)#路径
        sleep(5)
        self.browser.find_element(By.XPATH,'//*[@id="lists"]/li[1]/div/img').click()
        sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(5)
        
    def test_shop_sj(self):
        #商品上架
        #回到主框架
        self.browser.switch_to.default_content()
        #定位商品子框架
        iframe1=self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        #再进入商品子框架
        self.browser.switch_to.frame(iframe1)
        #点击仓库中商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        
        #获取列表
        shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list)!=0:
            #列表长度不为0,则上架
            #不循环，上架之后列表会刷新，固定区第一行
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            self.browser.find_element(By.LINK_TEXT,'确定').click()
    
    
        
    def test_edits(self):
    #回到主框架
        self.browser.switch_to.default_content()
    #定位商品子框架
        iframe1=self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
    #点击仓库中商品
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        #获取列表
        sleep(2)
        edits_shop=self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(edits_shop)==0:
            print("没有数据!!")
        else:
            sleep(1)
        #点击编辑
            self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[1]').click()
            sleep(2)
        #进入编辑子框架
            edits_iframe=self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
            self.browser.switch_to.frame(edits_iframe)
            sleep(1)
        #清空内容
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
            sleep(1)
        #输入内容
            nowday = datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('七宫商品'+nowday)
            sleep(1)
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[2]/div/input').send_keys(nowday)
            sleep(1)
        #第二个下拉框
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
            sleep(1)
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
            sleep(1)
        #第三个下拉框
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
            sleep(1)
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
            sleep(1)
        #卖点
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[4]/div/input').send_keys('人美心善')
            sleep(1)
        #主图
        #鼠标悬停
            dot=self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[1]/img')
            ActionChains(self.browser).move_to_element(dot).perform()
        #删除原图
            self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[1]/a').click()
            sleep(1)
        #点击选择图片
            self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        #进入选择图片框架
            upload_img_iframe_3=self.browser.find_element(By.XPATH,'/html/body/div[3]/div[2]/iframe')
            self.browser.switch_to.frame(upload_img_iframe_3)
            sleep(2)
            self.test_upload_img('E:\\软件测试\\2.jpg')
            
            #轮播图
            #回到主框架
            self.browser.switch_to.default_content()
            self.browser.switch_to.frame(iframe1)
            self.browser.switch_to.frame(edits_iframe)
            #鼠标悬停
            dot1=self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/ul/li/img')
            ActionChains(self.browser).move_to_element(dot1).perform()
            #删除原图
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/ul/li/a').click()
            sleep(1)
            #点击添加图片
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
            #进入添加图片框架
            upload_img_iframe_4=self.browser.find_element(By.XPATH,'/html/body/div[4]/div[2]/iframe')
            self.browser.switch_to.frame(upload_img_iframe_4)
            sleep(2)
            self.test_upload_img('E:\\软件测试\\2.jpg')
            
            
            #回到主框架
            self.browser.switch_to.default_content()
            self.browser.switch_to.frame(iframe1)
            self.browser.switch_to.frame(edits_iframe)
            #点击价格库存
            self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
            sleep(2)
            #定位价格、成本价、库存并输入数据
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').clear()
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').clear()
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').clear()
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').clear()
            sleep(2)
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('2.5')
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('0.5')
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('1.5')
            self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('1000')
            
            #回到商品子框架进行保存
            #回到主框架
            self.browser.switch_to.default_content()
            #再进入商品子框架
            self.browser.switch_to.frame(iframe1)
            #保存
            self.browser.find_element(By.LINK_TEXT,'保存').click()
            sleep(3)
            
            
            
        
        
        
    
    def test_logout(self):
            self.browser.switch_to.default_content()
        #鼠标悬停
            dt=self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
            ActionChains(self.browser).move_to_element(dt).perform()
            sleep(2)
            self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        
    
        
if __name__ =='__main__':
    LikeShopTest=LikeShopTest()
    LikeShopTest.test_1()
    LikeShopTest.test_shop_list()
    LikeShopTest.test_shop_add()  
    LikeShopTest.test_shop_sj()
    LikeShopTest.test_edits()
    LikeShopTest.test_logout()